<?php

namespace maotool\core\response;

use maotool\core\lang\HttpStatusCode;
use maotool\log\Console;
use JsonSerializable;

class JsonResult extends Result implements JsonSerializable
{

    /**
     * 状态
     */
    private $code;

    /**
     * 描述状态
     */
    private $name;

    /**
     * 状态描述
     */
    private $message;

    /**
     * 状态描述
     */
    private $msg;

    /**
     * 时间戳
     */
    private $timestamp;

    /**
     * 哈希值
     */
    private $hash;

    /**
     * 版本
     */
    private $version = "latest";

    /**
     * 接口
     */
    private $api;

    /**
     * 数据类型
     */
    private $type;

    /**
     * 元数据
     */
    private $metadata = [];

    /**
     * 成功
     */
    private $isSuccess;

    /**
     * 成功
     */
    private $success;

    /**
     * 日志编号
     */
    private $logId;

    /**
     * 布局
     */
    private $layout = [];

    /**
     * 接收的ip
     */
    private $ip;

    /**
     * 调试
     */
    private $debug = false;

    /**
     * 返回数据
     * @var mixed
     */
    private $data;

    /**
     * 携带日志
     * @var array
     */
    private $logs;

    /**
     * 语言
     */
    private $lang = "zh-cn";

    /**
     * 更多
     * @var array
     */
    private $more = [];

    /**
     * 列
     * @var array
     */
    private $column = [];

    /**
     * 搜索
     * @var
     */
    private $search;

    /**
     * 页面
     * @var
     */
    private $page;

    /**
     * 分页
     * @var
     */
    private $paginate;

    /**
     * 视图
     * @var
     */
    private $view;

    /**
     * 路由
     * @var
     */
    private $route;

    /**
     * 权限
     * @var
     */
    private $permissions;

    /**
     * 过期时间
     * @var
     */
    private $expireTime;

    /**
     * 表单
     * @var
     */
    private $form;

    /**
     * 选择
     * @var
     */
    private $select;

    /**
     * 树
     * @var
     */
    private $tree;

    /**
     * 超链接
     * @var
     */
    private $url;

    /**
     * 全部
     * @var
     */
    private $full;

    public function __construct($data = null)
    {
        $this->data = $data;
        $this->logs = Console::$lines;
    }

    public function getCode()
    {
        return $this->code;
    }

    public function setCode($code)
    {
        $this->code = $code;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getMsg()
    {
        return $this->msg;
    }

    public function setMsg($msg)
    {
        $this->msg = $msg;
        $this->message = $msg;
    }

    public function getMessage()
    {
        return $this->message;
    }

    public function setMessage($message)
    {
        $this->message = $message;
        $this->msg = $message;
    }

    public function getTimestamp()
    {
        return $this->timestamp;
    }

    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;
    }

    public function getHash()
    {
        return $this->hash;
    }

    public function setHash($hash)
    {
        $this->hash = $hash;
    }

    public function getType()
    {
        return $this->type;
    }

    public function setType($type)
    {
        $this->type = $type;
    }

    public function getVersion()
    {
        return $this->version;
    }

    public function setVersion($version)
    {
        $this->version = $version;
    }

    public function getApi()
    {
        return $this->api;
    }

    public function setApi($api)
    {
        $this->api = $api;
    }

    public function isDebug()
    {
        return $this->debug;
    }

    public function setDebug($debug)
    {
        $this->debug = $debug;
    }

    public function getMetadata()
    {
        return $this->metadata;
    }

    public function setMetadata($metadata)
    {
        $this->metadata = $metadata;
    }

    public function getIsSuccess()
    {
        return $this->isSuccess;
    }

    public function setIsSuccess($isSuccess)
    {
        $this->isSuccess = $isSuccess;
        $this->success = $isSuccess == 1;
    }

    public function getSuccess()
    {
        return $this->success;
    }

    public function setSuccess($success)
    {
        $this->success = $success;
        $this->isSuccess = $success ? 1 : 0;
    }

    public function getLogId()
    {
        return $this->logId;
    }

    public function setLogId($logId)
    {
        $this->logId = $logId;
    }

    public function getLayout()
    {
        return $this->layout;
    }

    public function setLayout($layout)
    {
        $this->layout = $layout;
    }

    public function getIp()
    {
        return $this->ip;
    }

    public function setIp($ip)
    {
        $this->ip = $ip;
    }

    public function getLang()
    {
        return $this->lang;
    }

    public function setLang($lang)
    {
        $this->lang = $lang;
    }

    public function getData()
    {
        return $this->data;
    }

    public function setData($data)
    {
        $this->data = $data;
    }

    public function debug($debug)
    {
        $this->setDebug($debug);
        return $this;
    }

    public function isSuccess($isSuccess)
    {
        $this->setIsSuccess($isSuccess);
        return $this;
    }

    public function success($success)
    {
        $this->setSuccess($success);
        return $this;
    }

    public function logId($logId)
    {
        $this->setLogId($logId);
        return $this;
    }

    public function lang($lang)
    {
        $this->setLang($lang);
        return $this;
    }

    public function code($code)
    {
        $this->setCode($code);
        return $this;
    }

    public function name($name)
    {
        $this->setName($name);
        return $this;
    }

    public function message($message)
    {
        $this->setMessage($message);
        return $this;
    }

    public function msg($msg)
    {
        $this->setMsg($msg);
        return $this;
    }

    public function timestamp($timestamp)
    {
        $this->setTimestamp($timestamp);
        return $this;
    }

    public function hash($hash)
    {
        $this->setHash($hash);
        return $this;
    }

    public function version($version)
    {
        $this->setVersion($version);
        return $this;
    }

    public function api($api)
    {
        $this->setApi($api);
        return $this;
    }

    public function type($type)
    {
        $this->setType($type);
        return $this;
    }

    public function ip($ip)
    {
        $this->setIp($ip);
        return $this;
    }

    public function data($data)
    {
        $this->setData($data);
        return $this;
    }

    public function layout($layout)
    {
        $this->setLayout($layout);
        return $this;
    }

    public function metadata($metadata)
    {
        $this->setMetadata($metadata);
        return $this;
    }

    public static function ok($data = null, $message = HttpStatusCode::OK["description"], $code = HttpStatusCode::OK["code"])
    {
        return self::suc($data, $message, $code);
    }

    public static function suc($data = null, $message = HttpStatusCode::OK["description"], $code = HttpStatusCode::OK["code"])
    {
        $r = new static();
        $r->setCode($code);
        $r->setMessage($message);
        $r->setName($message);
        $r->setTimestamp(time());
        $r->setSuccess(true);
        $r->setData($data);
        return $r;
    }


    public static function fail($data = null, $message = HttpStatusCode::INTERNAL_SERVER_ERROR["description"], $code = HttpStatusCode::INTERNAL_SERVER_ERROR["code"])
    {
        return self::err($data, $message, $code);
    }


    public static function err($data = null, $message = HttpStatusCode::INTERNAL_SERVER_ERROR["description"], $code = HttpStatusCode::INTERNAL_SERVER_ERROR["code"])
    {
        $r = new static();
        $r->setCode($code);
        $r->setMessage($message);
        $r->setName($message);
        $r->setTimestamp(time());
        $r->setSuccess(false);
        $r->setData($data);
        return $r;
    }

    public static function other($data = null, $message = null, $code = null)
    {
        $r = new static();
        $r->setCode($code);
        $r->setMessage($message);
        $r->setName($message);
        $r->setTimestamp(time());
        $r->setSuccess(false);
        $r->setData($data);
        return $r;
    }

    public static function unfound()
    {
        $r = new static();
        $r->setTimestamp(time());
        $r->setCode(HttpStatusCode::NOT_FOUND["code"]);
        $r->setMessage(HttpStatusCode::NOT_FOUND["description"]);
        $r->setName(HttpStatusCode::NOT_FOUND["name"]);
        return $r;
    }

    public static function syserror()
    {
        $r = new static();
        $r->setTimestamp(time());
        $r->setCode(HttpStatusCode::INTERNAL_SERVER_ERROR["code"]);
        $r->setMessage(HttpStatusCode::INTERNAL_SERVER_ERROR["description"]);
        $r->setName(HttpStatusCode::INTERNAL_SERVER_ERROR["name"]);
        return $r;
    }

    /**
     * @return array
     */
    public function getLogs()
    {
        return $this->logs;
    }

    /**
     * @param array $logs
     */
    public function setLogs($logs)
    {
        $this->logs = $logs;
    }

    /**
     * @return array
     */
    public function getMore()
    {
        return $this->more;
    }

    /**
     * @param array $more
     */
    public function setMore($more)
    {
        $this->more = $more;
    }

    /**
     * @return array
     */
    public function getColumn()
    {
        return $this->column;
    }

    /**
     * @param array $column
     */
    public function setColumn($column)
    {
        $this->column = $column;
    }

    /**
     * @return mixed
     */
    public function getRoute()
    {
        return $this->route;
    }

    /**
     * @param mixed $route
     */
    public function setRoute($route)
    {
        $this->route = $route;
    }

    /**
     * @return mixed
     */
    public function getView()
    {
        return $this->view;
    }

    /**
     * @param mixed $view
     */
    public function setView($view)
    {
        $this->view = $view;
    }

    /**
     * @return mixed
     */
    public function getPaginate()
    {
        return $this->paginate;
    }

    /**
     * @param mixed $paginate
     */
    public function setPaginate($paginate)
    {
        $this->paginate = $paginate;
    }

    /**
     * @return mixed
     */
    public function getPage()
    {
        return $this->page;
    }

    /**
     * @param mixed $page
     */
    public function setPage($page)
    {
        $this->page = $page;
    }

    /**
     * @return mixed
     */
    public function getSearch()
    {
        return $this->search;
    }

    /**
     * @param mixed $search
     */
    public function setSearch($search)
    {
        $this->search = $search;
    }

    /**
     * @return mixed
     */
    public function getPermissions()
    {
        return $this->permissions;
    }

    /**
     * @param mixed $permissions
     */
    public function setPermissions($permissions)
    {
        $this->permissions = $permissions;
    }

    /**
     * @return mixed
     */
    public function getExpireTime()
    {
        return $this->expireTime;
    }

    /**
     * @param mixed $expireTime
     */
    public function setExpireTime($expireTime)
    {
        $this->expireTime = $expireTime;
    }

    /**
     * @return mixed
     */
    public function getForm()
    {
        return $this->form;
    }

    /**
     * @param mixed $form
     */
    public function setForm($form)
    {
        $this->form = $form;
    }

    /**
     * @return mixed
     */
    public function getSelect()
    {
        return $this->select;
    }

    /**
     * @param mixed $select
     */
    public function setSelect($select)
    {
        $this->select = $select;
    }

    /**
     * @return mixed
     */
    public function getTree()
    {
        return $this->tree;
    }

    /**
     * @param mixed $tree
     */
    public function setTree($tree)
    {
        $this->tree = $tree;
    }

    /**
     * @return mixed
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * @param mixed $url
     */
    public function setUrl($url)
    {
        $this->url = $url;
    }

    /**
     * @return mixed
     */
    public function getFull()
    {
        return $this->full;
    }

    /**
     * @param mixed $full
     */
    public function setFull($full)
    {
        $this->full = $full;
    }

    public function jsonSerialize()
    {
        return [
            "code" => $this->code,
            "name" => $this->name,
            "message" => $this->message,
            "msg" => $this->msg,
            "timestamp" => $this->timestamp,
            "version" => $this->version,
            "api" => $this->api,
            "type" => $this->type,
            "metadata" => $this->metadata,
            "isSuccess" => $this->isSuccess,
            "success" => $this->success,
            "logId" => $this->logId,
            "layout" => $this->layout,
            "ip" => $this->ip,
            "debug" => $this->debug,
            "data" => $this->data,
            "lang" => $this->lang,
            "logs" => $this->logs,
            "more" => $this->more,
            "column" => $this->column,
            "form" => $this->form,
            "page" => $this->page,
            "paginate" => $this->paginate,
            "view" => $this->view,
            "search" => $this->search,
            "permissions" => $this->permissions,
            "route" => $this->route,
            "expireTime" => $this->expireTime,
            "select" => $this->select,
            "tree" => $this->tree,
            "url" => $this->url,
            "full" => $this->full,
        ];
    }

}